<template>
  <t-link
    v-permission="['system:order-designer:design-to-engineering']"
    theme="primary"
    @click="handleOpenDialog('update', { orderId: props.orderId})"
  >
    重新发起合同
  </t-link>
  <!-- 自定义对话框 -->
  <custom-dialog
    ref="customDialogRef"
    v-slot="{ formData }"
    :api-map="{ update: designToEngineering }"
    header=""
    :on-before-save-data="handleDialogSaveData"
    @request-success="handleDialogRequestSuccess"
    :update-sucess-tip="'false'"
  >
    确认重新发起合同？该操作将会<span style="color: #d54941;">不可逆</span>删除合同款项，是否继续？
    请输入"<span style="color: #d54941;">确认重新发起</span>"
    <t-form-item
      label="申请备注"
      name="extensionRemark"
      :rules="[{ required: true, message: '请输入确认重新发起' }]"
      label-width="0"
    >
      <t-textarea v-model:value="formData.extensionRemark" placeholder="请输入确认重新发起" />
    </t-form-item>
  </custom-dialog>
  <t-dialog v-model:visible="visible" attach="body" :on-confirm="onConfirmAnother" :on-cancel="onConfirmAnother">
      请到“合同信息”继续编辑提交审核
  </t-dialog>

</template>

<script setup lang="ts">
import { MessagePlugin } from 'tdesign-vue-next';
import { PropType, ref, useTemplateRef } from 'vue';

import {
  designToEngineering,
} from '@/api/order/design';
import { getUserListByCompanyIdAndRoleId } from '@/api/system/user';
import CustomDialog from '@/components/custom/dialog/index.vue';
import { useDictionaryStore, useUserStore } from '@/store';

// 字典仓库
const dictionaryStore = useDictionaryStore();
// props
const props = defineProps({
  // 订单id
  orderId: {
    type: Number,
    required: true,
  },
});
// emits
const emits = defineEmits(['requestSuccess']);
// 处理对话框保存数据
const handleDialogSaveData =(params: Record<string, any>)=>{
  console.log(params);
  
  if(params.extensionRemark.trim()==='确认重新发起') {
    delete params.extensionRemark
  } else {
    MessagePlugin.error('输入错误,请重新输入');
    return false;
  }
  
}
const visible = ref(false)
const onConfirmAnother = () => {
  visible.value = false;
  emits('requestSuccess', resultV);
};
// 自定义对话框ref
const customDialogRef = useTemplateRef('customDialogRef');
// 处理打开对话框
const handleOpenDialog = (mode: 'create' | 'read' | 'update', params: Record<string, any>) => {
  customDialogRef.value.open(mode, params);
};
const resultV = ref()
// 处理对话框请求成功
const handleDialogRequestSuccess = (result: Record<string, any>) => {
  visible.value = true
  resultV.value = result
};
</script>
